//****************************************************************************
// chainwriter.cpp
// implamentaion of the ChainWriter class.
//
// Author: Einat Zuker
//****************************************************************************
#include "parsing\chainwriter.h"

//----------------------------------------------------------------------------
// Ctor
// creats a new ChainWriter object with the file name given
//
// @param fileName
//		the name of the chain file to open for writing
//
// @throws ChainWriterExepetion
//		in case of failuer in opening the file
//----------------------------------------------------------------------------
ChainWriter::ChainWriter(string fileName)
{
	//TODO what to do in case file exsits? run over or error?
	m_file.open(fileName.c_str(), std::ios::out);
	if (m_file.fail())
	{
		string failStr = "failed to open the chain dile:" + fileName;
		throw (new ChainWriterExepetion(failStr));
	}
}

//----------------------------------------------------------------------------
// Dtor
// destroyes a ChainWriter object and closes the file assosiated with it.
// note: when closing a file it's content is flushed to the disk.
//----------------------------------------------------------------------------
ChainWriter::~ChainWriter()
{
	m_file.close();
}

//----------------------------------------------------------------------------
// setBinaryWordLength
//----------------------------------------------------------------------------
void ChainWriter::setBinaryWordLength(int n)
{
	m_file<<n<<" ";
	m_binaryWordLength = n;
}

//----------------------------------------------------------------------------
// setBinaryWordWeight
//----------------------------------------------------------------------------
void ChainWriter::setBinaryWordWeight(int w)
{
	m_file<<w<<" ";
}

//----------------------------------------------------------------------------
// setNumberOfGroups
//----------------------------------------------------------------------------
void ChainWriter::setNumberOfGroups(int newNumber)
{
	m_file<<newNumber<<"\n";
	m_numberOfGroups = newNumber;
}

//----------------------------------------------------------------------------
// setGroupSummery
// writes the group summery (all group sizes) to the chain file
//
// @param title
//		the titlte to give to the group summery. the title should not
//		contain any "\n" charcters or ":" charcters.
// @param groupSizes
//		an array of the group sizes. the first group size in the arry should
//		match the first group in the file and so on until the last group.
//
//----------------------------------------------------------------------------
void ChainWriter::setGroupSummery(string title, int* groupSizes)
{
	m_file<< title <<": ";
	
	if(groupSizes != NULL)
	{
		for(int i=0; i < m_numberOfGroups; i++)
		{
			m_file<<groupSizes[i]<<" ";
		}
	}

	m_file<<"\n";
}

//----------------------------------------------------------------------------
// setFirstVertex
//----------------------------------------------------------------------------
void ChainWriter::setFirstVertex(int groupId, vector<bool> *vertexName)
{
	m_file<<"\n"<<"# "<<groupId<<"\n";

	if(vertexName != NULL)
	{
		for(int i=0; i < m_binaryWordLength; i++)
		{
			m_file<<(*vertexName)[i];
		}
	}
	
	m_file<<"\n";
}

//----------------------------------------------------------------------------
// setNextVertex
//----------------------------------------------------------------------------
void ChainWriter::setNextVertex(vector<bool> *vertexName)
{
	if (vertexName != NULL)
	{
		for(int i=0; i < m_binaryWordLength; i++)
		{
			m_file<<(*vertexName)[i];
		}
	}
	
	m_file<<"\n";
}
